package com.longdaji.decoration.ui.signin;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.SignInBean;
import com.longdaji.decoration.data.bean.SignInDataBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class SignInPresenter extends RxPresenter<SignInContract.View> implements SignInContract.Presenter {
    private final static String TAG = SignInPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public SignInPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void signIn(String userId) {
        addSubscribe(mDataManager.signIn(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<SignInBean>handleResult())
                .subscribeWith(new CommonSubscriber<SignInBean>() {
                    @Override
                    public void onSuccess(SignInBean signInBean) {
                        Log.d(TAG, "SignInBean is" + signInBean);
                        if (mView != null && signInBean != null && signInBean.getMark() != null) {
                            mView.showSignInMsg(signInBean.getMark());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getSignData(String userId) {
        addSubscribe(mDataManager.getSignInData(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<SignInDataBean>handleResult())
                .subscribeWith(new CommonSubscriber<SignInDataBean>() {
                    @Override
                    public void onSuccess(SignInDataBean signInDataBean) {
                        Log.d(TAG, "SignInDataBean is" + signInDataBean);
                        if (mView != null && signInDataBean != null) {
                            mView.showSignData(signInDataBean);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }
}
